﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Moon.Common;
using GPI.Server;

public partial class Admin_SystemManage_AdminManage : System.Web.UI.UserControl
{
    UserService server = new UserService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    #region 按钮事件
    //进入添加页面
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        //若为0则为添加状态
        this.btnSave.CommandArgument = "0";
        //切换到信息编辑页面
        this.MultiView1.ActiveViewIndex = 1;
    }
    //更新或保持用户
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Model.UserInfo model = new Model.UserInfo();
        model.Id = this.btnSave.CommandArgument.ToInt(0);
        model.IpLog = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null?Request.ServerVariables["HTTP_X_FORWARDED_FOR"]:HttpContext.Current.Request.UserHostAddress;; 
        model.AdminUser = this.txtAdminUser.Text;
        model.AdminPass = this.txtPass1.Text.Equals(this.txtPass2.Text) ? this.txtPass2.Text : "";
        if ("".Equals(model.AdminPass))
        {
            MessageBox.Show(this.Page, "密碼為空或不匹配！");
        }
        else
        {
            bool isTrue = false;
            if ("0".Equals(this.btnSave.CommandArgument))
            {
                isTrue = server.Add(model);
            }
            else
            {
                isTrue = server.Update(model);
            }

            if (isTrue)
            {
                MessageBox.Show(this.Page, "操作成功！");
            }
            else
            {
                MessageBox.Show(this.Page, "操作失敗！");
            }
        }
        Bind();
    }
    //列表事件
    protected void repUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "edit":
                Model.UserInfo model = server.GetModel(e.CommandArgument.ObjectToInt(0));
                this.btnSave.CommandArgument = model.Id.ToString();
                this.txtAdminUser.Text = model.AdminUser;
                this.txtPass1.Text = model.AdminPass;
                this.txtPass2.Text = model.AdminPass;
                this.MultiView1.ActiveViewIndex = 1;
                break;
            case "delete":
                if (server.Delete(e.CommandArgument.ObjectToInt(0)))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                Bind();
                break;
        }

    }
    #endregion
    #region 页面动作
    //绑定操作
    private void Bind()
    {
        this.repUserList.Dispose();
        this.MultiView1.ActiveViewIndex = 0;
        this.repUserList.DataSource = server.GetAllList();
        this.repUserList.DataBind();
        Clear();
    }
    //清理操作
    private void Clear()
    {
        this.txtPass1.Dispose();
        this.txtPass2.Dispose();
        this.txtAdminUser.Dispose();
    }
    #endregion
}
    
